package yang.itcode.service.impl;

import cn.hutool.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yang.itcode.mapper.ProfitMapper;
import yang.itcode.model.pojo.Profit;
import yang.itcode.service.ProfitService;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
public class ProfitServiceImpl implements ProfitService {

    @Autowired
    private ProfitMapper profitMapper;

    @Override
    public JSONObject getProfitList(JSONObject obj) {
        if ("year".equals(obj.getStr("mode"))){
            LocalDate startDate = obj.get("startDate", LocalDate.class);
            LocalDate endDate = obj.get("endDate", LocalDate.class);
            String operationType = obj.getStr("operationType");
            JSONObject yearRange = obj.getJSONObject("yearRange");
            int startYear = yearRange.getInt("start");
            int endYear = yearRange.getInt("end");
            List<JSONObject> list = new ArrayList<>();
            for (int year = startYear; year <= endYear; year++) {
                LocalDate yearStart = LocalDate.of(year, 1, 1);
                LocalDate yearEnd = LocalDate.of(year, 12, 31);
                JSONObject condition = new JSONObject();
                condition.set("startDate", yearStart);
                condition.set("endDate", yearEnd);
                condition.set("operationType", operationType);
                condition.set("status", 1);
                List<Profit> profits = profitMapper.selectByCondition(condition);
                BigDecimal totalProfit = profits.stream()
                        .map(Profit::getValue)
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                JSONObject item = new JSONObject();
                item.set("label", String.valueOf(year));
                item.set("profit", totalProfit);
                list.add(item);
            }
            obj.set("list", list);
            return obj;
        } else if ("month".equals(obj.getStr("mode"))){
            LocalDate startDate = obj.get("startDate", LocalDate.class);
            LocalDate endDate = obj.get("endDate", LocalDate.class);
            String operationType = obj.getStr("operationType");
            JSONObject monthRange = obj.getJSONObject("monthRange");
            // 解析范围的YYYY-MM格式字符串
            String startMonthStr = monthRange.getStr("start");
            String endMonthStr = monthRange.getStr("end");
            String[] startParts = startMonthStr.split("-");
            String[] endParts = endMonthStr.split("-");
            int startYear = Integer.parseInt(startParts[0]);
            int startMonth = Integer.parseInt(startParts[1]);
            int endYear = Integer.parseInt(endParts[0]);
            int endMonth = Integer.parseInt(endParts[1]);
            List<JSONObject> list = new ArrayList<>();
            // 遍历每个月份
            for (int year = startYear; year <= endYear; year++) {
                int monthStart = (year == startYear) ? startMonth : 1;
                int monthEnd = (year == endYear) ? endMonth : 12;
                for (int month = monthStart; month <= monthEnd; month++) {
                    LocalDate monthStartDate = LocalDate.of(year, month, 1);
                    LocalDate monthEndDate = monthStartDate.withDayOfMonth(monthStartDate.lengthOfMonth());
                    JSONObject condition = new JSONObject();
                    condition.set("startDate", monthStartDate);
                    condition.set("endDate", monthEndDate);
                    condition.set("operationType", operationType);
                    condition.set("status", 1);
                    List<Profit> profits = profitMapper.selectByCondition(condition);
                    BigDecimal totalProfit = profits.stream()
                            .map(Profit::getValue)
                            .reduce(BigDecimal.ZERO, BigDecimal::add);
                    JSONObject item = new JSONObject();
                    item.set("label", String.format("%d-%02d", year, month));
                    item.set("profit", totalProfit);
                    list.add(item);
                }
            }
            obj.set("list", list);
            return obj;
        } else if ("day".equals(obj.getStr("mode"))){
            LocalDate startDate = obj.get("startDate", LocalDate.class);
            LocalDate endDate = obj.get("endDate", LocalDate.class);
            String operationType = obj.getStr("operationType");
            List<JSONObject> list = new ArrayList<>();
            for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
                JSONObject condition = new JSONObject();
                condition.set("startDate", date);
                condition.set("endDate", date);
                condition.set("operationType", operationType);
                condition.set("status", 1);
                List<Profit> profits = profitMapper.selectByCondition(condition);
                BigDecimal totalProfit = profits.stream()
                        .map(Profit::getValue)
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                JSONObject item = new JSONObject();
                item.set("label", date.toString());
                item.set("profit", totalProfit);
                list.add(item);
            }
            obj.set("list", list);
            return obj;
        }
        return null;
    }
}
